﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BattleView : MonoBehaviour
{
    public GameObject resultPage;
    public Button resultBtn;
    public Player player1;
    public Player player2;
    public Player enemy;

    public List<Skill> m_Skills = new List<Skill>();

    private int level;
    private int chapter;


    public void OnShow()
    {
        MessageCenter.Instance.Register(MessageConsts.BATTLE_START, OnBattleStart);
        this.resultPage.gameObject.SetActive(false);
    }

    public void OnHide()
    {
        MessageCenter.Instance.UnRegister(MessageConsts.BATTLE_START, OnBattleStart);
    }

    private void OnBattleStart(object param)
    {
        if (!this.gameObject.activeInHierarchy)
        {
            return;
        }
        var arr = param as object[];
        level = (int)arr[1];
        chapter = (int)arr[0];
        StartCoroutine(SkillRuner());
        Debug.Log("[BattleView][OnBattleStart] l:" + level + " c:" + chapter);
    }

    private void OnStopBattle()
    {
        this.resultPage.gameObject.SetActive(true);
    }

    private IEnumerator SkillRuner()
    {
        foreach (var skill in m_Skills)
        {
            skill.ResetSkill();
        }
        while (true)
        {
            foreach (var skill in m_Skills)
            {
                if (skill.Runner())
                {
                    yield return skill.Play();
                    skill.ResetSkill();
                }
                CheckStopBattle();
            }
            yield return null;
        }
    }

    private void Update()
    {
        //CheckStopBattle();
    }

    private void CheckStopBattle()
    {
        foreach (var skill in m_Skills)
        {
            if (skill.enemy.curHP < 0)
            {
                skill.enemy = skill.type == Skill.ZhenYingType.Player ? GetEnemy() : GetPlayer();
            }
        }
        var p = GetPlayer();
        var e = GetEnemy();
        if (p == null)
        {
            //失败
            ShowFailed();
        }
        if (e == null)
        {
            //成功
            ShowSuccess();
        }
    }

    private void ShowSuccess()
    {
        resultBtn.onClick.RemoveAllListeners();
        resultBtn.onClick.AddListener(() => { UIManager.Instance.ShowPage(UINames.XiuXingPage); });
        MessageCenter.Instance.SendMessage(MessageConsts.BATTLE_END);
        XiuXingModel.Instance.SetSuccess(chapter, level);
        this.resultPage.gameObject.SetActive(true);
        StopAllCoroutines();
    }

    private void ShowFailed()
    {
        StopAllCoroutines();
        resultBtn.onClick.RemoveAllListeners();
        resultBtn.onClick.AddListener(() => { UIManager.Instance.ShowPage(UINames.XiuXingPage); });
        MessageCenter.Instance.SendMessage(MessageConsts.BATTLE_END);
        this.resultPage.gameObject.SetActive(true);
    }

    private Player GetPlayer()
    {
        var list = new List<Player>() { player1, player2 };
        foreach (var p in list)
        {
            if (p.curHP >= 0)
            {
                return p;
            }
        }
        return null;
    }

    private Player GetEnemy()
    {
        var list = new List<Player>() { enemy };
        foreach (var p in list)
        {
            if (p.curHP >= 0)
            {
                return p;
            }
        }
        return null;
    }
}
